﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.Web.Security;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using ServiceStack.Text;
using System.Configuration;

namespace Framework.Library.Base.Helper
{
    public class LoginHelper : BaseClass
    {
        //public static PortalUser ValidateUser()

        public static void LoginUser(PortalUser info, bool _rememberAccount)
        {
            if (info != null)
            {
                FormsAuthentication.Initialize();
                var ticket = new FormsAuthenticationTicket(1, cfg.PrefixMember.VALUE + info.USERNAME, DateTime.Now,
                                                           DateTime.Now.AddDays(7), _rememberAccount, JsonSerializer.SerializeToString(info),
                                                           FormsAuthentication.FormsCookiePath);
                Trace.Write(FormsAuthentication.FormsCookiePath + FormsAuthentication.FormsCookieName);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                HttpContext.Current.Response.SetCookie(cookie);
                Dictionary<int, string> lstUser = HttpContext.Current.Application.GetUser();
                if (lstUser != null)
                {
                    if (!lstUser.ContainsKey(info.USERID))
                    {
                        lstUser.Add(info.USERID, info.DISPLAYNAME);
                        HttpContext.Current.Application.Add(cfg.PrefixMember.VALUE + "MEMBER_LOGIN", lstUser);
                    }
                }
                else
                {
                    lstUser = new Dictionary<int, string>();
                    lstUser.Add(info.USERID, info.DISPLAYNAME);
                    HttpContext.Current.Application.Add(cfg.PrefixMember.VALUE + "MEMBER_LOGIN", lstUser);
                }
                PortalUser updateLastLogin = new PortalUser();
                updateLastLogin.LASTVISIT = DateTime.Now;
                updateLastLogin.USERID = info.USERID;
                ManagerFactory.DAOEntity.Update(updateLastLogin);
            }
        }

        public static void LogoutUser()
        {
            Dictionary<int, string> lstUser = HttpContext.Current.Application.GetUser();
            if (lstUser != null)
            {
                lstUser.Remove(HttpContext.Current.Session.GetUser().USERID);
                HttpContext.Current.Application.Remove(cfg.PrefixMember.VALUE + "MEMBER_LOGIN");
                HttpContext.Current.Application.Add(cfg.PrefixMember.VALUE + "MEMBER_LOGIN", lstUser);
            }
            HttpContext.Current.Response.Cookies.Clear();
            //HttpContext.Current.Session.Remove(cfg.PrefixMember.value + "MEMBER_INFO");
            //HttpContext.Current.Session.Abandon();
            FormsAuthentication.SignOut();
        }
    }
}